﻿var handlerUrl = "Handlers/OrdersManagerHandler.ashx";
//页面初始化加载
$(function () {
    //显示全部未付款订单
    loadData(handlerUrl, { t: 0, s: 0, oid: 0 });
});

function loadData(url, param) {
    var r = Math.random();
    $.post(url + "?" + r, param, function (result) {
        $("tbody").empty();
        for (var i = 0; i < result.List.length; i++) {
            var order = result.List[i];
            var $tr = $("<tr></tr>");
            $tr.append("<td>" + order.OrderId + "</td>");
            $tr.append("<td>" + order.UserName + "</td>");
            $tr.append("<td>" + order.Phone + "</td>");
            $tr.append("<td>" + order.OrderDate + "</td>");
            $tr.append("<td>" + order.Total + "</td>");
            $tr.append("<td>" + order.OrderStatusName + "</td>");
            var buttons = "<a href='OrdersView.aspx?oid=" + order.OrderId + "' class='button-default pure-button pure-button-primary'>订单详情</a>";
            switch (order.OrderStatus) {
                case 0:
                    buttons += "<button type='button' class='button-default pure-button' onclick='doChangeOrder(" + order.OrderId + ",1);'>确认支付</button>";
                    break;
                case 1:
                    buttons += "<button type='button' class='button-default pure-button' onclick='doChangeOrder(" + order.OrderId + ",2);'>确认发货</button>";
                    break;
            }
            $tr.append("<td>" + buttons + "</td>");
            $("tbody").append($tr);
        }
        var btnTitles = ["未付款（" + result.UnPay + "）", "未发货（" + result.UnDelivery + "）", "待收货（" + result.UnGoods + "）", "已收货（" + result.IsComplete + "）", "已退货（" + result.IsReturn + "）"]
        $("#orderButtons").empty();
        for (var j = 0; j < btnTitles.length; j++) {
            var btnContainer = $("<div class='pure-u-1-5'></div>");
            //创建按钮
            var btn = $("<button type='button'></button>");
            //添加样式
            btn.addClass("button-default pure-button");
            if (j == param.s) {
                btn.addClass("pure-button-primary");
            }
            //设置按钮提示文本
            btn.text(btnTitles[j]);
            btn.on("click", { status: j },function (e) {                
                loadData(handlerUrl, { t: 0, s: e.data.status, oid: 0 });
            });
            btnContainer.append(btn);
            $("#orderButtons").append(btnContainer)
        }
    }, "json");
}
function doChangeOrder(orderId, newStatus) {
    //更改订单状态
    loadData(handlerUrl, { t: 1, s: newStatus, oid: orderId });
}
